import axios from 'axios'

export default {
    namespaced: true,
    state() {
        return {
            list: []
        }
    },
    mutations: {
        goodsList(state, arr) {
            console.log(arr);
            state.list = arr
        },
        checkState(state, { value, id }) {
            state.list[state.list.findIndex(item => item.id === id)].goods_state = value
        },
        checkAll(state, value) {
            state.list.forEach(item => item.goods_state = value)
        }
    },
    actions: {
        async dataRequest(ctx, url) {
            let arr = await (await axios.get(url)).data.list
            ctx.commit('goodsList', arr)
        }
    },
    getters: {
        allCount(state) {
            let sum = 0
            state.list.forEach(item => {
                if (item.goods_state) {
                    sum += item.goods_count
                }
            })
            return sum
        },
        allPrice(state) {
            let sum = 0
            state.list.forEach(item => {
                if (item.goods_state) {
                    sum += item.goods_count * item.goods_price
                }
            })
            return sum
        }
    }
}